<template>
    <div class="app-container">
        <el-table
            :data="reData"
            style="width: 100%"
            border
            v-loading="listLoading"
            highlight-current-row
            fit
            element-loading-text="加载数据中..."
        >
            <el-table-column label="序号" min-width="60" align="center">
                <template slot-scope="scope">
                    {{ scope.$index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="项目名称" min-width="200" align="center">
                <template slot-scope="scope">
                    <el-link type="primary" :href="scope.row.url" target="_blank">{{ scope.row.name }}</el-link>
                </template>
            </el-table-column>
            <el-table-column label="项目描述" min-width="300" align="center">
                <template slot-scope="scope">
                    {{ scope.row.description.toString() }}
                </template>
            </el-table-column>
            <el-table-column label="预览图" width="150" align="center">
                <template slot-scope="scope">
                    <el-image :src="scope.row.thumb" :preview-src-list="[scope.row.thumb]"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <el-tooltip content="Github" placement="top">
                        <el-button
                            @click="handleOpenGithub(scope.row)"
                            icon="el-icon-view"
                            type="info"
                            circle
                            size="mini"
                        ></el-button>
                    </el-tooltip>
                    <el-tooltip content="编辑" placement="top">
                        <el-button
                            @click="handleEditProject(scope.row)"
                            type="primary"
                            icon="el-icon-edit"
                            circle
                            size="mini"
                        ></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button
                            @click="handleDeleteProject(scope.row)"
                            type="danger"
                            icon="el-icon-delete"
                            circle
                            size="mini"
                        ></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <!-- 编辑的弹出框 -->
        <el-dialog title="请编辑该项目信息" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="项目名称">
                    <el-input v-model="form.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="项目描述（每项描述以英文,分割）">
                    <el-input v-model="form.description" type="textarea"></el-input>
                </el-form-item>
                <el-form-item label="链接">
                    <el-input v-model="form.url"></el-input>
                </el-form-item>
                <el-form-item label="Github地址">
                    <el-input v-model="form.github"></el-input>
                </el-form-item>
                <el-form-item label="预览图">
                    <upload v-model="form.thumb" :style="{ width: '178px' }"></upload>
                </el-form-item>
                <el-form-item label="排序等级">
                    <el-select v-model="form.order" placeholder="分类等级">
                        <el-option label="1" value="1"></el-option>
                        <el-option label="2" value="2"></el-option>
                        <el-option label="3" value="3"></el-option>
                        <el-option label="4" value="4"></el-option>
                        <el-option label="5" value="5"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleConfirmEdit">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getProject, setProject, deleteProject } from "@/api/project.js"
import { url } from "@/urlConfig.js"
import Upload from "@/components/Upload/index.vue"
export default {
    components: { Upload },
    data() {
        return {
            listLoading: false,
            data: [],
            dialogFormVisible: false,
            form: {
                name: "",
                description: "",
                url: "",
                github: "",
                thumb: "",
                order: 1
            }
        }
    },
    computed: {
        reData() {
            return this.data.map((item) => {
                if (item.thumb.startsWith(url)) {
                    return item
                } else {
                    item.thumb = url + item.thumb
                    return item
                }
            })
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        async fetchData() {
            this.listLoading = true
            const res = await getProject()
            this.data = res.data
            this.listLoading = false
        },
        handleEditProject(row) {
            this.dialogFormVisible = true
            this.form = { ...row, description: row.description.toString() }
        },
        handleDeleteProject(row) {
            this.$confirm("此操作会彻底删除, 是否继续?", "是否删除项目", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(async () => {
                    await deleteProject(row.id)
                    this.fetchData()
                    this.$message.success("删除成功")
                })
                .catch(() => {
                    this.$message.error("删除失败")
                })
        },
        // 确认编辑
        async handleConfirmEdit() {
            const obj = { ...this.form }
            obj.description = this.form.description.split(",")
            obj.order = parseInt(this.form.order)
            await setProject(obj.id, obj)
            this.dialogFormVisible = false
            this.fetchData()
            this.$message.success("修改成功")
        },
        // 打开github新窗口
        handleOpenGithub(row) {
            window.open(row.github)
        }
    }
}
</script>

<style lang="less" scoped></style>
